package algorithms;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;

public class Bzip2 implements LosslessCompressionAlgorithm 
{

	private String _extension = "bzip2";
	
	@Override
	public void encode(File filenameIn, File filenameOut) throws IOException {
		
		FileInputStream fileIn = new FileInputStream(filenameIn);
		BZip2CompressorOutputStream fileOut = new BZip2CompressorOutputStream(new FileOutputStream(filenameOut));
		
		byte[] buffer = new byte[8192];
		int nrOfBytesRead = 0;
		while ((nrOfBytesRead = fileIn.read(buffer)) != -1)
		{
			fileOut.write(buffer, 0, nrOfBytesRead);
		}
		fileIn.close();
		fileOut.close();
	}

	@Override
	public void decode(File fileIn, File fileOut) throws IOException {

	}

	@Override
	public String getFileExtension() {
		return _extension;
	}

	@Override
	public void setFileExtension(String name) {
		_extension = name;	
	}

}
